package com.hyf.okhttp3demo.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hyf.okhttp3demo.Config;
import com.hyf.okhttp3demo.R;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class PostRequestActivity extends AppCompatActivity {

    private EditText edtUserName, edtPassWord;
    private Button mButton;
    private TextView tvResult;

    private OkHttpClient mOkHttpClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_request);
        initView();
        initListener();
        mOkHttpClient = new OkHttpClient();
    }

    private void initListener() {
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName = edtUserName.getText().toString().trim();
                String passWord = edtPassWord.getText().toString().trim();
                if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(passWord)) {
                    Toast.makeText(PostRequestActivity.this, "用户名/密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                FormBody formBody = new FormBody.Builder().addEncoded("userName", userName).add("passWord", passWord).build();

                Request request = new Request.Builder()
                        .url(Config.API.URL_POST)
                        .post(formBody)
                        .build();
                mOkHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e("PostRequestActivity", e.getLocalizedMessage());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String result = response.body().string();
                        Log.e("PostRequestActivity", result);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tvResult.setText(result);
                            }
                        });
                    }
                });

            }
        });
    }

    private void initView() {
        edtUserName = (EditText) findViewById(R.id.edt_userName);
        edtPassWord = (EditText) findViewById(R.id.edt_passWord);
        mButton = (Button) findViewById(R.id.btn_start_login);
        tvResult = (TextView) findViewById(R.id.tv_login_result);
    }


}
